package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.event.EventArgs;
import com.iptv.grape.event.data.LiveData;
import com.iptv.grape.net.Entity.ChannelData;
import com.iptv.grape.ui.control.ad.ListViewUI;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;
import com.iptv.grape.util.ReadJsonFile;
import com.iptv.grape.util.ToastUtil;
import com.iptv.grape.video.XMediaPlayer;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.iptv.grape.bu.GrapeContext.curGrapeContext;

// 频道实体模型
public class Channel implements Comparable<Channel>,IListViewShowItem{

    // 当前的模式
    private static String curType = "live";

    public static String getCurType() {
        return curType;
    }

    public static void setCurType(String curType) {
        Channel.curType = curType;
    }

    // 频道ID
    private String keyID;
    // 频道号
    private int channelNo;
    // 频道名称
    private String name;
    // 频道简称
    private String sName;
    // 别名
    private String alName;
    // 图标地址
    private String logo;
    // 直播播放地址(单播地址)
    private String uAddress;
    // 直播播放地址(组播地址)
    private String mAddress;
    // 回看单播地址
    private String ruAddress;
    // 回看组播地址
    private String rmAddress;
    // 时移单播地址
    private String tuAddress;
    // 时移组播地址
    private String tmAddress;
    // 排序
    private int sort;
    // 站点编码
    private String siteCode;
    // 频道分类编码
    private ArrayList<String> categoryCodeList = new ArrayList();


    // 前一个频道
    private Channel previous;

    // 后一个频道
    private Channel next;



    public Channel getPrevious() {
        return previous;
    }

    public void setPrevious(Channel channel) {
        previous = channel;
    }

    public Channel getNext() {
        return next;
    }

    public void setNext(Channel channel) {
        next = channel;
    }

    public String getKeyID() {
        return keyID;
    }

    public int getChannelNo() {
        return channelNo;
    }

    public String getName() {
        return name;
    }

    public String getsName() {
        return sName;
    }

    public String getAlName() {
        return alName;
    }

    public String getLogo() {
        return logo;
    }

    public String getuAddress() {
        return uAddress;
    }

    public String getmAddress() {
        return mAddress;
    }

    public String getRuAddress() {
        return ruAddress;
    }

    public String getRmAddress() {
        return rmAddress;
    }

    public String getTuAddress() {
        return tuAddress;
    }

    public String getTmAddress() {
        return tmAddress;
    }

    @Override
    public String getCode() {
        return this.channelNo + "";
    }

    public int getSort() {
        return sort;
    }

    @Override
    public String getText() {
        return this.name;
    }

    // 单击频道列表
    @Override
    public void itemClick(List<IListViewShowItem> dataSouce, int pos, ListViewUI listViewUI) {
        // 执行换台操作
        this.playChannel();
        // 上报数据 列表+确定键
        LiveData liveData = new LiveData(this, "3", "23", "0");
        EventBus.getDefault().post(new EventArgs(AppConstant.DataLive, AppConstant.DataAcquisition, liveData));
    }
    // 立即播放当前的直播频道
    public void playChannel(){

//        AppLog.getCurLog().out(this.getKeyID());
        // 执行换台操作
        XMediaPlayer mediaPlayer = GrapeContext.curGrapeContext().getCurMediaPlayer();
        if(mediaPlayer != null){
            GrapeContext.curGrapeContext().getChannelDLinkList().setChannelHis(this);
            mediaPlayer.stop();
            mediaPlayer.play(GrapeContext.curGrapeContext().getCurActivity(), this.getPlayUrl());
            GrapeContext.curGrapeContext().setCurPlayType("Live");
            GrapeContext.curGrapeContext().setCurLiveType("Live");
            List<ChannelBill> bills = this.getCurChannelBillList();
            curGrapeContext().getCurActivity().showTopTitleDelayedClose(this.getName(),this.getChannelNo() + "",
                    bills.get(0),bills.get(1));
        }
    }

    // 获得当前正在播出的节目单和下一个即将播出的节目单
    public List<ChannelBill> getCurChannelBillList(){
        List<ChannelBill> bills = GrapeContext.curGrapeContext().getBillList().getBillsByChannelID(this.keyID);
        ChannelBill nowChannelBill = null;
        ChannelBill nextChannelBill = null;
        if(bills != null && bills.size() > 0) {
            for(int index = 0; index < bills.size(); index++){
                ChannelBill channelBill = bills.get(index);
                if(channelBill.getBillType().equals("now")){
                    nowChannelBill = channelBill;
                    if(index + 1 < bills.size()){
                        nextChannelBill = bills.get(index + 1);
                    }
                    break;
                }
            }
        }
        List<ChannelBill> reValList = new ArrayList<ChannelBill>();
        reValList.add(nowChannelBill);
        reValList.add(nextChannelBill);
        return reValList;
    }

    // 获得当前正在播出的节目单和下一个即将播出的节目单
    public ChannelBill getCurChannelBill(){
        List<ChannelBill> bills = GrapeContext.curGrapeContext().getBillList().getBillsByChannelID(this.keyID);
        ChannelBill nowChannelBill = null;

        if(bills != null && bills.size() > 0) {
            for(int index = 0; index < bills.size(); index++){
                ChannelBill channelBill = bills.get(index);
                if(channelBill.getBillType().equals("now")){
                    nowChannelBill = channelBill;
                    break;
                }
            }
        }
        return nowChannelBill;
    }

    // 进入回看模式
    // 播放指定的节目单
    public void playChannelBill(ChannelBill bill) {
        if (AppConfig.getAppConfig().isCanBackLive()) {
            XMediaPlayer mediaPlayer = GrapeContext.curGrapeContext().getCurMediaPlayer();
            if (mediaPlayer != null) {
                String playUrl = this.getRuAddress() + bill.getArgs();
                GrapeContext.curGrapeContext().getChannelDLinkList().setChannelHis(this);
                mediaPlayer.stop();
                mediaPlayer.play(GrapeContext.curGrapeContext().getCurActivity(), playUrl);
                // 直播类型
                GrapeContext.curGrapeContext().setCurPlayType("Live");
                // 回看模式
                curGrapeContext().setCurLiveType("Back");
            }
        }
    }

    // 进入时移模式
    public void playTimeMove(int timeArgs){
        if (AppConfig.getAppConfig().isCanTimeLive()) {
            XMediaPlayer mediaPlayer = GrapeContext.curGrapeContext().getCurMediaPlayer();
            if (mediaPlayer != null) {
                String playUrl = this.getTuAddress() + timeArgs;
                AppLog.getCurLog().out(playUrl);
                mediaPlayer.stop();
                mediaPlayer.play(GrapeContext.curGrapeContext().getCurActivity(), playUrl);

            }
        }
    }

    // 获得下级节目单
    @Override
    public List<IListViewShowItem> getChList() {
        List<IListViewShowItem> list = new ArrayList<IListViewShowItem>();

        if(curType.equals("live")) {
            List<ChannelBill> bills = GrapeContext.curGrapeContext().getBillList().getBillsByChannelID(this.keyID);
            if(bills != null && bills.size() > 0) {
                list.addAll(bills);
            }
        }
        else {
            List<ChannelBillTimeData> bills = CommUtil.getChannelBillTimeDataList(this);
            list.addAll(bills);
        }
        return list;
    }
    // 获得一个频道的指定日期的节目单
    public List<ChannelBill> getChannelBillList(String dateString){
        List<ChannelBill> bills = GrapeContext.curGrapeContext().getBillList().getBillsByChannelID(this.keyID,dateString);
        return bills;
    }

    @Override
    public ListViewStyle getListViewStyle(){
        if(AppConstant.isTvStb()) {
            return new ListViewStyle(140,"channelListItem",
                    "Channel",9,4,3,55,null);
        }else{
            return new ListViewStyle(140,"channelListItem",
                    "Channel",11,5,3,55,null);
        }

    }

    public List<String> getCategoryCodeList() {
        return categoryCodeList;
    }

    public String getSiteCode() {
        return siteCode;
    }



    public Channel(String keyID, String channelNo,
                   String name, String sName, String alName, String logo,
                   String uAddress, String mAddress, String ruAddress,
                   String rmAddress, String tuAddress, String tmAddress, int sort, String siteCode,String categoryCode) {
        this.keyID = keyID;
        this.channelNo = Integer.parseInt(channelNo);
        this.name = name;
        this.sName = sName;
        this.alName = alName;
        this.logo = logo;
        this.uAddress = uAddress;
        this.mAddress = mAddress;
        this.ruAddress = ruAddress;
        this.rmAddress = rmAddress;
        this.tuAddress = tuAddress;
        this.tmAddress = tmAddress;
        this.sort = sort;
        this.siteCode = siteCode;
        this.categoryCodeList.add(categoryCode);
    }

    // 获得播放地址
    public String getPlayUrl(){
        return this.uAddress;
    }


    public Channel(ChannelData channelData){
        this(channelData.getKeyID(),channelData.getChannelNo(),channelData.getName(),channelData.getsName(),
                channelData.getAlName(),channelData.getLogo(),channelData.getuAddress(),channelData.getmAddress(),
                channelData.getRuAddress(),channelData.getRmAddress(),channelData.getTuAddress(),channelData.getTmAddress(),
                channelData.getSort(),channelData.getSiteCode(),channelData.getCategoryCode());
    }

    // 查找是否存在该分类列表
    public boolean isExistsCategoryCode(String categoryCode){
        for (int i = 0; i < this.categoryCodeList.size(); i++){
            if(this.categoryCodeList.get(i).equals(categoryCode)){
                return true;
            }
        }
        return false;
    }
    // 添加一个频道分类编码到列表中
    public void addCategoryCode(String categoryCode){
        this.categoryCodeList.add(categoryCode);
    }

    @Override
    public int compareTo(Channel channel) {
        return this.channelNo - channel.channelNo;
    }

    // 转换为播放历史数据
    public ChannelRecords toChannelRecords(){
        ChannelRecords channelRecords = new ChannelRecords();
        channelRecords.setChannelID(this.getKeyID());
        channelRecords.setChannelNo(this.getChannelNo());
        channelRecords.setName(this.getName());
        channelRecords.setLogo(this.getLogo());
        channelRecords.setPlayUrl(this.getPlayUrl());
        return channelRecords;
    }



}
